﻿//时间格式化
Date.prototype.format = function (format) //author: meizz 
{
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(),    //day 
        "h+": this.getHours(),   //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter 
        "S": this.getMilliseconds() //millisecond 
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

function StrToDate(JSONDateString) {
    var date = new Date(parseInt(JSONDateString.replace("/Date(", "").replace(")/", ""), 10));
    return date.format("yyyy-MM-dd hh:mm:ss");
}
//删除事件
function delClick(obj) {
    if (confirm("确认要删除？")) {
        var delid = $(obj).attr("id");
        $.ajax({
            type: "post",
            url: "/ashx/ProduceMaterial.ashx",
            data: { delbarid: $(obj).attr("alt"), },
            success: function (msg) {
            //0是已经出库 -1 是barcode不存在 1是删除成功 -2是删除barcode失败 -3删除库存失败
                if(msg == "1"){
                    alert("删除成功");
                    $(obj).parent().parent().remove();
                }else if(msg=="0"){
                    alert("已经出库,不能删除");
                }else{
                    alert("删除失败");
                }

            }
        });
        
    }
}

//修改事件 条形码重复
function updateClick(obj) {

    var barid = $(obj).attr("alt");
    $.ajax({
        type: "post",
        url: "/ashx/ProduceMaterial.ashx",
        data: { "barid": barid },
        success: function (msg) {
            var barobj = eval('(' + msg + ')');

            $("#txtgc").val(barobj.goodscode);
            $("#txtgn").val(barobj.goodsname);
            $("#txtgwt").val(barobj.gwt);
            $("#txtqty").val(barobj.qty);
            $("#lblInDate").html(StrToDate(barobj.createdate));
            $("#txtremark").val(barobj.remark);
            $("#lblbarcode").html(barobj.barcodevalue);

        }
    });

}

$(document).ready(function () {
    //物品编码选取
    $("#txtgc").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "post",
                url: "/ashx/AutoProductMaterial.ashx",
                data: { "goods": $("#txtgc").val(), "Outid": $(".hidOutid").val() },
                dataType: "json",
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.label + " " + item.value,
                            value: item.value,
                            lab: item.label,
                            goodsid: item.rubid
                        }
                    }));
                },
                error: function (result, status) {
                    alert('result:' + result + "; status:" + status);
                }
            });

        },
        select: function (event, ui) {
            $("#txtgc").val(ui.item.lab);
            $("#txtgn").val(ui.item.value);
            $(".hidgdid").val(ui.item.goodsid);
            return false;
        },

        minLength: "1"
    });

    //净重计算

    $("#txttare2,#txtgwt,#txttare1").blur(function () {
        if ($("#txttare1").val() != "" && $("#txtgwt").val() != "" && $("#txttare2").val() != "") {
            var gwt = parseFloat($("#txtgwt").val());
            var t1 = parseFloat($("#txttare1").val());
            var t2 = parseFloat($("#txttare2").val());
            var nwt = (gwt - t1 - t2).toFixed(2);
            $("#txtnwt").html(nwt);
        }
    });

    //为空验证
    function CheckInput() {
        var strErr = "";
        if ($("#txtgc").val() == "") strErr += "物品编码不能为空！\n";
        if ($("#txtgn").val() == "") strErr += "物品名称不能为空！\n";
        if ($("#txtqty").val() == "" || isNaN($("#txtqty").val())) strErr += "数量必须输入数字！\n";

        return strErr;
    }

    var timer = null; function do_click(event) {
        clearTimeout(timer); // 这里加一句是为了兼容 Gecko 的浏览器 /
        if (event.detail == 2)
            return;
        // 同上句的作用 
        timer = setTimeout(function () {
            // click 事件的处理 
        }, 300);
    }



    $("#btnSaveBarcode").dblclick()
    {
        clearTimeout(timer);
    }

    //插入操作
    $("#btnSaveBarcode").click(function () {
        InsertBar();
    });

    function InsertBar() {
        //插入验证
        var strErr = CheckInput();
        if (strErr != "") {
            $("#txtgc").focus();
            alert(strErr);
            return;
        } else {
            checkGoodsName();
        }
    }



    //插入条形码事件
    function InsertBarCode() {

        var jston = {
            "insid": $(".hiddenInSid").val(),
            "loadnum": $(".hidLoadnum").val(),
            "outid": $(".hidOutid").val(),
            "outsno": $(".hidoutsno").val(),
            "goodscode": $("#txtgc").val(),
            "goodsname": $("#txtgn").val(),
            "nwt": $("#txtgwt").val(),
            "qty": $("#txtqty").val(),
            "barcode": $("#lblbarcode").html(),
            "remark": $("#txtremark").val(),
            //    "islocak": $("#chkIsLock").attr("checked"),
            "inDepart": $(".hidInDepart").val(),
            "outDepart": $(".hidOutDepart").val(),
            "month": $(".hidmonth").val(),
            "goodsid": $(".hidgdid").val(),
            "oper": $(".hiddenOper").val(),
            "weight": $(".lblTotalweight").html()

        };
        $.ajax({
            type: "post",
            url: "/ashx/ProduceMaterial.ashx",
            data: jston,
            dataType: "json",
            success: function (msg) {
                if (msg != -2) {

                    //var obj =  msg ;
                    $(".labInDate").html(StrToDate(msg[1].instoragetime));
                    // $(".txtin").val(msg[1].wrlno);
                    var tr = "<tr>";
                    tr += "<td>" + ($('table tr').length - 1) + "</td>";
                    tr += "<td class='barcodevalue'>" + msg[0].goodscode + "</td>";
                    tr += "<td>" + msg[0].goodsname + "</td>";
                    tr += "<td>" + msg[0].nwt + "</td>";
                    tr += "<td>" + msg[0].qty + "</td>";
                    tr += "<td>" + msg[0].remark + "</td>"; //备注
                    tr += "<td>" + StrToDate(msg[1].instoragetime) + "</td>";
                    tr += "<td>" + msg[0].barcodevalue + "</td>";   //条码
                    tr += "<td><input type='checkbox' class='flase' title='" + msg[0].id + "' " + GetIsLock(msg[0].isLocak) + "  />锁定</td>"; //锁定
                    tr += "<td><img src='/images/jian.png' alt='" + msg[0].id + "' width='16' onclick='javascript:delClick(this)'  height='17''/></td>";
                    tr += "<td><img src='/images/icons/button_update.gif' alt='" + msg[0].id + "' onclick='javascript:updateClick(this)' height='17' class='update'/></td>";
                    tr += "<td><a href='/Storage/BarcodeSmallImg.aspx?barCodeID=" + msg[0].id + "' target='_blank'>条码重打</a></td>";
                    tr += "</tr>";

                    //清空
                    $("#txtgc").val("");
                    $("#txtgn").val("");
                    $("#txtgwt").val("0");
                    $("#txtqty").val("");
                    $("#txtremark").val("");
                    $("#lblbarcode").html("");
                    $("#chkIsLock").attr("checked", false);
                    $(".hiddenInSid").val(msg[0].InStrorageID);
                    
                    $(".lblInsTotal").html(msg[1].instotal);
                    $(".lblInsno").html(msg[1].wrlno);
                    //打印小标签
                    window.open("/Storage/BarcodeSmallImg.aspx?barcodeid=" + msg[0].id, "_blank");
                    if (msg[0] != null) {
                        $("#tab").append(tr);
                    }
                }
                else {
                    alert("重量不能超过出库生产的重量");
                }
            }
        });

    }

    //验证称重页面的物品编码，废物编码，库位
    function checkGoodsName() {
        $.ajax({
            type: "post",
            url: "/ashx/IsGoodCode.ashx?",
            data: { goodCodeid: $("#txtgc").val(), checkStatus: "0" },
            success: function (msg) {
                switch (msg) {
                    case "-1":
                        alert("不能成功调用！");
                        break;
                    case "0":
                        InsertBarCode();
                        break;
                    default:
                        alert("不正常数据！");
                        break;
                }
            }
        });
    }
    //判断是否选中
    function GetIsLock(islock) {
        if (islock == true)
            return "checked";
        else
            return "";
    }
    //checkbox绑定点击事件
    $(".flase").each(function () {
        $(this).click(function () {
            var id = $(this).attr("title");
            var isLock = "0";
            if ($(this).attr("checked") == "checked")
                isLock = "1";
            else
                isLock = "0";
            updataLock(id, isLock);
        });

    });

    function updataLock(id, Islock) {
        $.ajax({
            type: "post",
            url: "/ashx/ProduceMaterial.ashx",
            data: { barcodeid: id, islock: Islock },
            success: function (msg) {
                alert(msg);
            }
        });
    }


});
